package cn.toys.modular.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.toys.base.auth.context.LoginContextHolder;
import cn.toys.base.auth.model.LoginUser;
import cn.toys.modular.entity.ProductType;
import cn.toys.modular.entity.ProductTypeExample;
import cn.toys.modular.mapper.ProductTypeMapper;
import cn.toys.modular.service.IProductTypeService;
import cn.toys.sys.core.constant.factory.ConstantFactory;
import cn.toys.sys.modular.system.entity.Role;
import cn.toys.sys.modular.system.model.RoleDto;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品类型表 服务实现类
 * </p>
 *
 * @author toys
 * @since 2021-02-26
 */
@Service
public class ProductTypeServiceImpl implements IProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public ProductType selectById(Integer productTypeId) {
        ProductTypeExample productTypeExample = new ProductTypeExample();
        productTypeExample.createCriteria().andIdEqualTo(productTypeId.longValue());
        return productTypeMapper.selectOneByExample(productTypeExample);
    }

    @Override
    public List<ProductType> selectByName(String name) {
        ProductTypeExample productTypeExample = new ProductTypeExample();
        if(StringUtils.isNotEmpty(name)){
            productTypeExample.createCriteria().andNameLike(name);
        }
        List<ProductType> productTypes = productTypeMapper.selectByExample(productTypeExample);
        if(productTypes != null && productTypes.size()>0){
            List<Long> createUserIds = productTypes.stream().map(ProductType::getCreateUserId).collect(Collectors.toList());
            List<Long> updateUserIds = productTypes.stream().map(ProductType::getUpdateUserId).collect(Collectors.toList());
            createUserIds.addAll(updateUserIds);
            Map<Long,String> userMap = ConstantFactory.me().getUserNameMap(createUserIds);
            for(ProductType productType: productTypes){
                if(userMap.containsKey(productType.getCreateUserId())){
                    String createUserName = userMap.get(productType.getCreateUserId());
                    productType.setCreateUserName(createUserName);
                }

                if(userMap.containsKey(productType.getUpdateUserId())){
                    String updateUserName = userMap.get(productType.getUpdateUserId());
                    productType.setUpdateUserName(updateUserName);
                }
            }
        }

        return productTypes;
    }

    @Override
    public Long getCountByName(String name) {
        ProductTypeExample productTypeExample = new ProductTypeExample();
        if(StringUtils.isNotEmpty(name)){
            productTypeExample.createCriteria().andNameLike(name);
        }
        return productTypeMapper.countByExample(productTypeExample);
    }

    @Override
    public Integer insert(ProductType productType) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        if(productType != null){
            Date current = new Date();
            productType.setUpdateTime(current);
            productType.setUpdateUserId(user.getId());
            productType.setCreateTime(current);
            productType.setCreateUserId(user.getId());
        }
        return productTypeMapper.insert(productType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(ProductType productType) {
        if (ToolUtil.isOneEmpty(productType, productType.getId(), productType.getName(), productType.getLabel())) {
            throw new RequestEmptyException();
        }
        LoginUser user = LoginContextHolder.getContext().getUser();
        if(productType != null){
            Date current = new Date();
            productType.setUpdateTime(current);
            productType.setUpdateUserId(user.getId());
        }
        ProductTypeExample productTypeExample = new ProductTypeExample();
        productTypeExample.createCriteria().andIdEqualTo(productType.getId());
        ProductType old = productTypeMapper.selectOneByExample(productTypeExample);
        BeanUtil.copyProperties(productType, old);
        return productTypeMapper.updateByExampleSelective(old,productTypeExample);
    }

    @Override
    public Integer deleteById(Integer productTypeId) {
        ProductTypeExample productTypeExample = new ProductTypeExample();
        productTypeExample.createCriteria().andIdEqualTo(productTypeId.longValue());
        return productTypeMapper.deleteByExample(productTypeExample);
    }


}
